package com.gmrz.uap.authenticators;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gmrz.uap.cfg.SystemConst;
import com.gmrz.uap.common.*;
import com.gmrz.uap.dict.DictService;
import com.gmrz.uap.model.Authenticators;
import com.gmrz.uap.model.Dict;
import com.gmrz.uap.model.UasConfig;
import com.gmrz.uap.resource.ResourceInterceptor;
import com.gmrz.uap.uasconfig.UasConfigService;
import com.gmrz.uap.util.DateUtil;
import com.gmrz.uap.util.HttpClientUtil;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AuthenticatorsController extends ControllerSupport {
    /**
     * 元数据主页面
     */
    @Before({ResourceInterceptor.class, AuthTypeInterceptor.class, TransTypeInterceptor.class, OperTypeInterceptor.class})
    public void index() {
        Dict dict = DictService.me.findById("24753805db4e41d6a894d7c7d45b3957");
        String certManageflag = "0";
        if(null != dict){
            certManageflag = dict.getDictText();
        }
        setAttr("certManageflag",certManageflag);
        render("index" + SystemConst.VIEW_EXTENDSION);
    }

    /**
     * 根据前台发过来的参数进行分页,查询
     */
    public void list() {

        int page = getParaToInt(SystemConst.DATATABLE_PAGINATE_OFFSET, SystemConst.DATATABLE_PAGINATE_OFFSET_DEFAULT);
        int rows = getParaToInt(SystemConst.DATATABLE_PAGINATE_LIMIT, SystemConst.DATATABLE_PAGINATE_LIMIT_DEFAULT);

        String sort = getPara(SystemConst.DATATABLE_PAGINATE_SORT, SystemConst.DEFAULT_ORDERBY_CREATETS);
        String order = getPara(SystemConst.DATATABLE_PAGINATE_ORDER, SystemConst.DATATABLE_PAGINATE_ORDER_DEFAULT);

        Authenticators authenticators = getModel(Authenticators.class);

        authenticators.put(ConditionConst.CONDITION_DEVICE_TYPE, getPara(ConditionConst.CONDITION_DEVICE_TYPE,null));//设备类型
        authenticators.put(ConditionConst.CONDITION_OS_TYPE, getPara(ConditionConst.CONDITION_OS_TYPE,null));//系统类型
        authenticators.put(ConditionConst.CONDITION_START_TIME, getPara(ConditionConst.CONDITION_START_TIME,null));//开始时间
        authenticators.put(ConditionConst.CONDITION_END_TIME, getPara(ConditionConst.CONDITION_END_TIME,null));//结束时间

        authenticators.setTenantId(getCookie(SystemConst.COOKIE_TENANT_ID));

        Page<Authenticators> pages = AuthenticatorsService.me.findListPaginate(authenticators, page, rows, sort, order);

        result.put(SystemConst.DATATABLE_PAGINATE_TOTAL, pages.getTotalRow());
        result.put(SystemConst.DATATABLE_PAGINATE_ROWS, pages.getList());

        render(responseJsonRender(result));
    }

    public void certFrozen(){
        String ids = getPara("ids");

        int from = 3;
        int to =5;
        int n = Authenticators.dao.updateCertExtend(ids,from,to);
        if(n > 0){
            flag = true;
            message = "冻结成功";
        }else{
            flag = false;
            message = "冻结失败";
        }
        result.put(SystemConst.RESPONSE_RESULT_FLAG, flag);
        result.put(SystemConst.RESPONSE_RESULT_MESSAGE, message);

        render(responseJsonRender(result));
    }

    public void certThaw(){
        String ids = getPara("ids");

        int to = 3;
        int from =5;
        int n = Authenticators.dao.updateCertExtend(ids,from,to);
        if(n > 0){
            flag = true;
            message = "解冻成功";
        }else{
            flag = false;
            message = "解冻失败";
        }
        result.put(SystemConst.RESPONSE_RESULT_FLAG, flag);
        result.put(SystemConst.RESPONSE_RESULT_MESSAGE, message);

        render(responseJsonRender(result));
    }

    public void certCancel(){
        flag = false;
        message = "注销失败";
        String ids = getPara("ids");

        Authenticators authenticators = Authenticators.dao.findById(ids);
        if(null != authenticators){
            if(authenticators.getStatus() == 1){
                String custNo = authenticators.getCustNo();
                String appID = authenticators.getTenantId();
                String transNo = custNo+appID+DateUtil.getOrderNum();
                String authType = authenticators.getAuthType();
                String transType = authenticators.getTransType();
                String deviceID = authenticators.getDeviceId();
                String from = "01";
                String cancelReason = "5";
                String request = "{\"context\":{\"userName\":\""+custNo+"\",\"appID\":\""+appID+"\",\"transNo\":\""+transNo+"\",\"transType\":\""+transType+"\",\"authType\":\""+authType+"\",\"deviceID\":\""+deviceID+"\",\"from\":\""+from+"\",\"cancelReason\":\""+cancelReason+"\"}}";
                String url = UasConfig.dao.getValueByKey("uaf.url");
                //String url = DictService.me.findById("388be214198a47048e9844311e24aa31").getDictText();
                String response = HttpClientUtil.post(url+"/reg/delete",request,"application/fido+uaf");
                JSONObject jsonObject = JSON.parseObject(response);
                int code = jsonObject.getIntValue("statusCode");

                if(1200 == code){
                    flag = true;
                    message = "注销成功";
                }else {
                    message = "注销失败,code:"+code;
                }

            }else{
                message = "注销失败,认证器状态不正确";
            }
        }else {
            message = "注销失败,没找到认证器";
        }
        result.put(SystemConst.RESPONSE_RESULT_FLAG, flag);
        result.put(SystemConst.RESPONSE_RESULT_MESSAGE, message);

        render(responseJsonRender(result));
    }

}
